package com.djb.TvSite;

import java.io.File;
import java.util.List;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
//	private static final String path = "/media/tvserver/videos";
	private static final String path = "/videos";
	private List<File> videos;
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		 videos = VideoFiles.getVideoFiles(new File(path));
		 model.addAttribute("videos", videos );
		return "home";
	}
	
	@RequestMapping(value="/**/playvideo/{videoid}")
	public String playvideo(Model model, @PathVariable String videoid){
		try{
		int index = Integer.valueOf(videoid);
		File video = videos.get(index);
		VideoFiles.playVideo(video);
		model.addAttribute("video", video);
		return "play";
		}
		catch(Exception e){
			return "redirect:/";
			
		}
		
	}
	@RequestMapping(headers ={"Accept=application/json"}, value="/command", method= RequestMethod.POST)
	@ResponseBody
	public void sendCommand(String command){
		try{
			
			VideoFiles.sendCommand(command);
		}
		catch(Exception e){
			
		}
		
	}
	
}
